package com.macro.mall.portal.thread;

import com.alibaba.fastjson.JSONArray;
import com.github.binarywang.wxpay.bean.notify.WxScanPayNotifyResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.macro.mall.common.utils.DateUtil;
import com.macro.mall.domain.CreateProductParam;
import com.macro.mall.mapper.OmsOrderItemMapper;
import com.macro.mall.mapper.OmsOrderMapper;
import com.macro.mall.model.OmsOrder;
import com.macro.mall.model.OmsOrderItem;
import com.macro.mall.model.OmsOrderItemExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

public class ToServer extends Thread {
    private WxPayService wxService;
    @Autowired
    public ToServer(WxPayService wxService) {
        this.wxService = wxService;
    }
    @Autowired
    private OmsOrderMapper orderMapper;
    @Autowired
    private OmsOrderItemMapper orderItemMapper;
    @Value("${wx.notifyUrl}")
    private String NOTIFYURL;

    private String json;
    //需要什么参数就自己定义
    public ToServer(String json) {
        this.json = json;
    }
    /**
     * 执行定时请求
     */
    @Override
    public void run() {
        WxScanPayNotifyResult result = JSONArray.parseObject(json, WxScanPayNotifyResult.class);
        OmsOrder omsOrder = orderMapper.selectByPrimaryKey(Long.parseLong(result.getProductId()));
        OmsOrderItemExample omsOrderItemExample = new OmsOrderItemExample();
        OmsOrderItemExample.Criteria criteria = omsOrderItemExample.createCriteria();
        criteria.andOrderIdEqualTo(Long.parseLong(result.getProductId()));
        List<OmsOrderItem> omsOrderItems = orderItemMapper.selectByExample(omsOrderItemExample);
        String productName = omsOrderItems.get(0).getProductName();

//        log.info("result.getIsSubscribe()：{}", result.getIsSubscribe());
        WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = new WxPayUnifiedOrderRequest();
        wxPayUnifiedOrderRequest.setMchId(result.getMchId());
        wxPayUnifiedOrderRequest.setAppid(result.getAppid());
        wxPayUnifiedOrderRequest.setOpenid(result.getOpenid());
        wxPayUnifiedOrderRequest.setProductId(result.getProductId());
        wxPayUnifiedOrderRequest.setBody("好货集-" + productName);
        wxPayUnifiedOrderRequest.setAttach(omsOrder.getOrderSn());
        BigDecimal multiply = omsOrder.getTotalAmount().multiply(new BigDecimal(100));
        wxPayUnifiedOrderRequest.setTotalFee(multiply.intValue());
        wxPayUnifiedOrderRequest.setTimeStart(DateUtil.dateTimeNow());
        wxPayUnifiedOrderRequest.setTradeType("NATIVE");
        wxPayUnifiedOrderRequest.setOutTradeNo(result.getProductId());
        wxPayUnifiedOrderRequest.setNotifyUrl(NOTIFYURL);
        //用 getLocalHost() 方法创建的InetAddress的对象
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        wxPayUnifiedOrderRequest.setSpbillCreateIp(address.getHostAddress());

        try {
            WxPayUnifiedOrderResult wxPayUnifiedOrderResult = this.wxService.unifiedOrder(wxPayUnifiedOrderRequest);
        } catch (WxPayException e) {
            e.printStackTrace();
        }
    }
}